﻿@using eBusiness.App_DAL
@model eBusiness.Models.Product
@{
    ViewBag.Title = "Edit Product";
    Layout = "../Shared/_Layout.cshtml";
    var _unitOfWork = new UnitOfWork();
    var categoryIds = Model.Categories.Aggregate(string.Empty, (current, cat) => current + (cat.ID + ","));
}
<h2>
    Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="../../../../Scripts/jquery-ui-1.8.11.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".date-picker").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'mm-dd-yy',
            showButtonPanel: false,
            gotoCurrent: false
        });
        $('#ProductImageUploader').change(function () {
            $("#tempFile").html("<table><tr>");
            //$("#thumbImageChoose").html("<table><tr>");
            var count = 0;
            for (var i = 0; i < this.files.length; i++) {
                if (this.files && this.files[i]) {
                    var reader = new window.FileReader();
                    reader.onload = function (e) {
                        $("#tempFile").append('<td><div style="max-width:120px; height:82px; overflow:hidden; margin-left:10px;"><img style="height:80px; border:solid 1px #80ABD5" src="' + e.target.result + '" alt="" /></div><div style="text-align:center;"><input style="max-width:100px;" type="radio" id = "thumbImage' + count + '" name = "ThumbImages" class="rdThumbImage" value="' + count + '" />thumbimage<div></td>');
                        // $("#thumbImageChoose").append('<td><span><input style="max-width:100px;" type="radio" id = "thumbImage' + i + '" name = "thumbImages" /></span></td>');
                        count++;
                    };
                    reader.readAsDataURL(this.files[i]);
                }
            }
            $("#tempFile").html("</tr></table>");
            // $("#thumbImageChoose").html("</tr></table>");
        });
        $('#ProductThumbImageUploader').change(function () {
            $("#tempThumbFile").html("");
            for (var i = 0; i < this.files.length; i++) {
                if (this.files && this.files[i]) {
                    var reader = new window.FileReader();
                    reader.onload = function (e) {
                        $("#tempThumbFile").append('<div style="float:left; max-width:120px; height:82px; overflow:hidden; margin-left:10px;"><img style="height:80px; border:solid 1px #80ABD5" src="' + e.target.result + '" alt="" /></div>');
                    };
                    reader.readAsDataURL(this.files[i]);
                }
            }
        });

        $("#discountDateRow").hide();
        $("#discountNumberRow").hide();
        $("#isHomePageDateRow").hide();
        $("#isHotDateRow").hide();

        if ($("#IsHot").is(":checked")) {
            $("#isHotDateRow").show();
        } else {
            $("#isHotDateRow").hide();
        }

        $("#IsHot").change(function (e) {
            if ($("#IsHot").is(":checked")) {
                $("#isHotDateRow").show();
            } else {
                $("#isHotDateRow").hide();
            }
        });

        if ($("#IsHomepage").is(":checked")) {
            $("#isHomePageDateRow").show();
        } else {
            $("#isHomePageDateRow").hide();
        }
        $("#IsHomepage").change(function (e) {
            if ($("#IsHomepage").is(":checked")) {
                $("#isHomePageDateRow").show();
            } else {
                $("#isHomePageDateRow").hide();
            }
        });

        if ($("#IsDiscount").is(":checked")) {
            $("#discountDateRow").show();
            $("#discountNumberRow").show();
        } else {
            $("#discountDateRow").hide();
            $("#discountNumberRow").hide();
        }

        $("#IsDiscount").change(function (e) {
            if ($("#IsDiscount").is(":checked")) {
                $("#discountDateRow").show();
                $("#discountNumberRow").show();
            } else {
                $("#discountDateRow").hide();
                $("#discountNumberRow").hide();
            }
        });

        $(".categoryChoose").change(function (e) {
            var catIds = $("#categoryIds").val();
            if ($("#" + e.target.id).is(":checked")) {
                catIds += e.target.id.replace("cat", "") + ",";
            }
            else {
                var id = (e.target.id.replace("cat", "") + ",");
                catIds = catIds.replace(id, "");
            }
            $("#categoryIds").val(catIds);
        });

        $(".rdThumbImage").live("change", function (e) {
            var id = e.target.id.replace("thumbImage", "");
            //var id = $("input [type='radio'] :checked").val();
            $("#ThumbImage").val(id);
        });

    });
</script>
@using (Html.BeginForm("Edit", "Products", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    @Html.Hidden("categoryIds", categoryIds)
    @Html.Hidden("ThumbImage", Model.ThumbImage)
    @Html.HiddenFor(model => model.Images)
    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.CreatedBy)
    @Html.HiddenFor(model => model.CreatedDate)
    <fieldset>
        <legend>Product</legend>
        <fieldset>
            <table class="detailsTables">
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.QuantityInStock)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.QuantityInStock)
                        @Html.ValidationMessageFor(model => model.QuantityInStock)
                    </td>
                </tr>
                <tr>
                    <td>
                        Category
                    </td>
                    <td>
                        <table border="0" cellpadding="0" cellspacing="0">
                            @{
                               var categories = _unitOfWork.CategoryRepository.Get(c => c.IsDeleted == false).ToList();
                               var numberOfRow = categories.Count() % 3 == 0 ? categories.Count() / 3 : categories.Count() / 3 + 1;
                               for (int i = 0; i < numberOfRow; i++)
                               {
                                    <tr>
                                        @for (int j = i * 3; j < (i + 1) * 3 && j < categories.Count(); j++)
                                        {
                                            var cat = categories[j];
                                            var id = "cat" + cat.ID;
                                            var isExisted = false;
                                            foreach (var proCate in Model.Categories)
                                            {
                                                if (proCate.ID == cat.ID)
                                                {
                                                    isExisted = true;
                                                    break;
                                                }
                                            }

                                            if (isExisted)
                                            {
                                            <td style="width: 250px">
                                                <input type="checkbox" id="@id" class="categoryChoose"  checked="true"/>
                                                <span>@cat.Name</span>
                                            </td>
                                            }
                                            else
                                            {
                                            <td style="width: 250px">
                                                <input type="checkbox" id="@id" class="categoryChoose" />
                                                <span>@cat.Name</span>
                                            </td>
                                            }
                                        }
                                    </tr>
                               }
                            }
                        </table>
                        @*@foreach (var cat in _unitOfWork.CategoryRepository.Get(c => c.IsDeleted == false))
                        {
                            var id = "cat" + cat.ID;
                            var isExisted = false;
                            foreach (var proCate in Model.Categories)
                            {
                                if (proCate.ID == cat.ID)
                                {
                                    isExisted = true;
                                    break;
                                }
                            }

                            if (isExisted)
                            {
                            <span>
                                <input type="checkbox" id="@id" class="categoryChoose" checked="true" />
                                @cat.Name
                            </span>
                            }
                            else
                            {
                            <span>
                                <input type="checkbox" id="@id" class="categoryChoose" />
                                @cat.Name
                            </span>
                            }
                        }*@
                    </td>
                </tr>
            </table>
        </fieldset>
        <fieldset>
            <table class="detailsTables">
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.Name)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.Name)
                        @Html.ValidationMessageFor(model => model.Name)
                    </td>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.Code)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.Code)
                        @Html.ValidationMessageFor(model => model.Code)
                    </td>
                </tr>
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.Color)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.Color)
                        @Html.ValidationMessageFor(model => model.Color)
                    </td>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.Size)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.Size)
                        @Html.ValidationMessageFor(model => model.Size)
                    </td>
                </tr>
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.MadeFrom)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.MadeFrom)
                        @Html.ValidationMessageFor(model => model.MadeFrom)
                    </td>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.MadeBy)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.MadeBy)
                        @Html.ValidationMessageFor(model => model.MadeBy)
                    </td>
                </tr>
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.Type)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.Type)
                        @Html.ValidationMessageFor(model => model.Type)
                    </td>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.StatusID, "Status")
                    </td>
                    <td class="editor-field">
                        @Html.DropDownListFor(model => model.StatusID, (SelectList)@ViewBag.Status)
                        @Html.ValidationMessageFor(model => model.StatusID)
                    </td>
                </tr>
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.Manufacture)
                    </td>
                    <td class="editor-field">
                        @Html.DropDownListFor(model => model.ManufactureID, (SelectList)@ViewBag.Manufacture)
                        @Html.ValidationMessageFor(model => model.Manufacture.ID, "*")
                    </td>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.Warranty)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.Warranty)
                        @Html.ValidationMessageFor(model => model.Warranty, "*")
                    </td>
                </tr>
            </table>
        </fieldset>
        <fieldset>
            <table class="detailsTables">
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.Price)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.Price)
                        @Html.ValidationMessageFor(model => model.Price)
                    </td>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.OriginalPrice)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.OriginalPrice)
                        @Html.ValidationMessageFor(model => model.OriginalPrice)
                    </td>
                </tr>
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.IsDiscount)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.IsDiscount)
                        @Html.ValidationMessageFor(model => model.IsDiscount)
                    </td>
                </tr>
                <tr id="discountDateRow">
                    <td class="editor-label">
                        @Html.LabelFor(model => model.DiscountStartDate)
                    </td>
                    <td class="editor-field">
                        @Html.TextBoxFor(model => model.DiscountStartDate, new { @class = "date-picker" })
                        @Html.ValidationMessageFor(model => model.DiscountStartDate)
                    </td>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.DiscountEndDate)
                    </td>
                    <td class="editor-field">
                        @Html.TextBoxFor(model => model.DiscountEndDate, new { @class = "date-picker" })
                        @Html.ValidationMessageFor(model => model.DiscountEndDate)
                    </td>
                </tr>
                <tr id="discountNumberRow">
                    <td class="editor-label">
                        @Html.LabelFor(model => model.DiscountByNumber)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.DiscountByNumber)
                        @Html.ValidationMessageFor(model => model.DiscountByNumber)
                    </td>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.DiscountByPercent)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.DiscountByPercent)
                        @Html.ValidationMessageFor(model => model.DiscountByPercent)
                    </td>
                </tr>
            </table>
        </fieldset>
        <fieldset>
            <table class="detailsTables">
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.IsHot)
                    </td>
                    <td class="editor-field">
                        @Html.EditorFor(model => model.IsHot)
                        @Html.ValidationMessageFor(model => model.IsHot)
                    </td>
                </tr>
                <tr id="isHotDateRow">
                    <td class="editor-label">
                        @Html.LabelFor(model => model.IsHotStartDate)
                    </td>
                    <td class="editor-field">
                        @Html.TextBoxFor(model => model.IsHotStartDate, new { @class = "date-picker" })
                        @Html.ValidationMessageFor(model => model.IsHotStartDate)
                    </td>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.IsHotEndDate)
                    </td>
                    <td class="editor-field">
                        @Html.TextBoxFor(model => model.IsHotEndDate, new { @class = "date-picker" })
                        @Html.ValidationMessageFor(model => model.IsHotEndDate)
                    </td>
                </tr>
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.IsHomepage)
                    </td>
                    <td class="editor-field">
                        @Html.CheckBoxFor(model => model.IsHomepage)
                        @Html.ValidationMessageFor(model => model.IsHomepage)
                    </td>
                </tr>
                <tr id="isHomePageDateRow">
                    <td class="editor-label">
                        @Html.LabelFor(model => model.IsHomepageStartDate)
                    </td>
                    <td class="editor-field">
                        @Html.TextBoxFor(model => model.IsHomepageStartDate, new { @class = "date-picker" })
                        @Html.ValidationMessageFor(model => model.IsHomepageStartDate)
                    </td>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.IsHomepageEndDate)
                    </td>
                    <td class="editor-field">
                        @Html.TextBoxFor(model => model.IsHomepageEndDate, new { @class = "date-picker" })
                        @Html.ValidationMessageFor(model => model.IsHomepageEndDate)
                    </td>
                </tr>
            </table>
        </fieldset>
        <fieldset>
            <table>
                @*<tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.ThumbImage)
                    </td>
                    <td class="editor-field">
                        <input type="file" id="ProductThumbImageUploader" name="file" style="width: 100%" />
                        <div id="tempThumbFile" style="margin-top: 10px;">
                        </div>
                        @Html.ValidationMessageFor(model => model.ThumbImage)
                    </td>
                </tr>*@
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.Images)
                    </td>
                    <td class="editor-field">
                        <input type="file" id="ProductImageUploader" name="file" multiple="multiple" style="width: 100%" />
                        <div id="tempFile" style="margin-top: 10px;">
                            @if (!string.IsNullOrEmpty(Model.Images))
                            {
                                <table>
                                    <tr>
                                        @{var images = Model.Images.Split(':');
                                          foreach (var image in images)
                                          {
                                              if (!string.IsNullOrEmpty(image))
                                              {
                                                  var imgsrc = image;
                                            <td>
                                                <div style="max-width: 120px; height: 82px; overflow: hidden; margin-left: 10px;">
                                                    <img style="height:80px; border:solid 1px #80ABD5" src="@imgsrc" alt="" />
                                                </div>
                                            </td>
                                              }
                                          }
                                        }
                                    </tr>
                                </table>
                            }
                        </div>
                        @Html.ValidationMessageFor(model => model.Images)
                    </td>
                </tr>
            </table>
        </fieldset>
        <fieldset>
            <table class="detailsTables">
                <tr>
                    <td class="editor-label">
                        @Html.LabelFor(model => model.Description)
                    </td>
                    <td class="editor-field">
                        @Html.TextAreaFor(model => model.Description)
                        @Html.ValidationMessageFor(model => model.Description)
                    </td>
                </tr>
            </table>
        </fieldset>
        <p>
            <input type="submit" value="Update" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
